//
//  HROAuthViewController.swift
//  microBlog
//
//  Created by HR_W on 16/5/22.
//  Copyright © 2016年 admin. All rights reserved.
//

import UIKit
import SVProgressHUD


class HROAuthViewController: UIViewController,UIWebViewDelegate {

    let webView = UIWebView()
    
    
    override func loadView() {
    
        view = webView
        
        webView.delegate = self
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setNavBar()
        
        loadOAuthPage()
    }

   


}

//打开授权界面
extension HROAuthViewController {

    private func loadOAuthPage() {
    
        let urlString = "https://api.weibo.com/oauth2/authorize?"+"client_id="+client_id+"&redirect_uri="+redirect_uri
        
        let url = NSURL(string: urlString)!
        
        let request = NSURLRequest(URL: url)
        
        webView.loadRequest(request)
    }
    

}


//设置导航条 监听按钮点击事件
extension HROAuthViewController {
    
    @objc private func close() {
    
    dismissViewControllerAnimated(true, completion: nil)
    }
    
    @objc private func test() {
    //js 动态注入
        let jsString = "document.getElementById('userId').value = '15857137056',document.getElementById('passwd').value = 'haorui330124'"
        
        webView.stringByEvaluatingJavaScriptFromString(jsString)
    }
    
    
    private func setNavBar() {
    
        navigationItem.leftBarButtonItem = UIBarButtonItem(target: self, action: #selector(close), title: "关闭")
    
        navigationItem.rightBarButtonItem = UIBarButtonItem(target: self, action: #selector(test), title: "测试账号")
    }
    
    
}

extension HROAuthViewController {

    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        let urlString = request.URL?.absoluteString ?? ""
        // print(urlString)
        let successFlag = "code="
        
        if urlString.containsString(successFlag) {
            
            let range = urlString.rangeOfString(successFlag)
            let code = urlString.substringFromIndex(range!.endIndex)
            
            //print("code==="+code,"urlstring==="+urlString)
            
           HRUserAccountViewModel.shareAccountViewModel.loadAccessToken(code, finished: { (isSuccess) in
            
            //发送通知
            if !isSuccess {
                SVProgressHUD.showErrorWithStatus(AppErrorTip)
            
            }
            
            self.dismissViewControllerAnimated(false, completion: { 
               
                NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: nil)
                
            })
            })
            
            return false
        }
        
        
        
        return true
    }


}
